package views;

import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author Aman
 */
public class Popup extends javax.swing.JFrame {

    private String[] fieldsdata;
    private int number;
    private JTextField[] fields;
    private JLabel[] label;

    @SuppressWarnings("OverridableMethodCallInConstructor")
    public Popup() {
        initComponents();
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
    }

    public void setPopupWindow(final int fnumber, final int tab, final String[] data) {
        String[] labelindex = {"User Id", "Username", "Password", "AC", "Heaters", "Lights", "User Type", "ID", "Level", "Power", "Amount"};
        
        resetPopup();
        int x = 10, y = 25;
        this.number = fnumber;
        this.fields = new JTextField[number];
        label = new JLabel[number];
        
        fieldsdata = new String[number];
        for (int i = 0; i < number; i++) {
            fields[i] = new JTextField(data[i]);
            
            if (i == 0)  fields[0].setEditable(false);
            
            if (tab == 0)  label[i] = new JLabel(labelindex[i]);
            
            if (tab == 1 || tab == 2 || tab == 3)  label[i] = new JLabel(labelindex[i + 7]);
            
            if (tab == 3)  label[2] = new JLabel(labelindex[10]);

            label[i].setBounds(x, (y * i + 10), 80, 20);
            fields[i].setBounds(x * 10, (y * i + 10), 80, 25);

            jPanel.add(fields[i]);
            jPanel.add(label[i]);
        }
        setVisible(true);
    }

    public void setPopupWindow(final int fnumber, final int tab) {
        String[] labelindex = {"Username", "Password", "AC", "Heaters", "Lights", "User Type", "Level", "Power", "Amount"};
        
        resetPopup();
        int x = 10, y = 25;
        this.number = fnumber;
        this.fields = new JTextField[number];
        label = new JLabel[number];
        
        fieldsdata = new String[number];
        for (int i = 0; i < number; i++) {
            fields[i] = new JTextField();
            
            if (tab == 0)  label[i] = new JLabel(labelindex[i]);
            
            if (tab == 1 || tab == 2 || tab == 3)  label[i] = new JLabel(labelindex[i + 6]);
            
            if (tab == 3)  label[1] = new JLabel(labelindex[8]);

            label[i].setBounds(x, (y * i + 10), 80, 20);
            fields[i].setBounds(x * 10, (y * i + 10), 80, 25);

            jPanel.add(fields[i]);
            jPanel.add(label[i]);
        }
        setVisible(true);
    }

    public String[] getFieldsData() {
        fieldsdata = new String[number];
        for (int i = 0; i < number; i++) {
            fieldsdata[i] = fields[i].getText().toString();
        }
        resetPopup();
        return fieldsdata;
    }

    public void resetPopup() {
        for (int i = 0; i < number; i++) {
            jPanel.remove(fields[i]);
            jPanel.remove(label[i]);
        }
        jPanel.validate();
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel = new javax.swing.JPanel();
        addUpdate = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        addUpdate.setText("Add/Update");

        javax.swing.GroupLayout jPanelLayout = new javax.swing.GroupLayout(jPanel);
        jPanel.setLayout(jPanelLayout);
        jPanelLayout.setHorizontalGroup(
            jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelLayout.createSequentialGroup()
                .addGap(0, 125, Short.MAX_VALUE)
                .addComponent(addUpdate))
        );
        jPanelLayout.setVerticalGroup(
            jPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelLayout.createSequentialGroup()
                .addGap(0, 195, Short.MAX_VALUE)
                .addComponent(addUpdate))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void UpdateListener(ActionListener Update) {
        addUpdate.addActionListener(Update);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addUpdate;
    private javax.swing.JPanel jPanel;
    // End of variables declaration//GEN-END:variables
}
